package com.hskn.hss.module.carPlan.controller;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.core.utils.file.FileTypeUtils;
import com.hskn.hss.core.utils.poi.ExcelUtil;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.ExcelReaderDataException;
import com.hskn.hss.module.attend.entity.Attend;
import com.hskn.hss.module.carPlan.entity.CarPlan;
import com.hskn.hss.module.carPlan.mapper.CarPlanMapper;
import com.hskn.hss.module.carPlan.service.CarPlanService;
import com.hskn.hss.module.carPlan.to.CarPlanExcelReaderTO;
import com.hskn.hss.module.carPlan.to.CarPlanTo;
import com.hskn.hss.module.carPlan.to.CarWorkPlanCountTo;
import com.hskn.hss.module.carPlan.vo.*;
import com.hskn.hss.module.common.ienum.ExcelTypeEnum;
import com.hskn.hss.module.tlthresholdtrans.mapper.TlThresholdTransMapper;
import com.hskn.hss.module.workgroup.entity.WorkGroup;
import com.hskn.hss.module.workgroup.mapper.WorkGroupMapper;
import com.hskn.hss.module.workrule.entity.WorkRule;
import com.hskn.hss.module.workrule.mapper.WorkRuleMapper;
import com.hskn.hss.task.HssTask;
import com.hskn.hss.task.WorkTask;
import com.hskn.hss.utils.Tools;
import com.hskn.hss.utils.page.PageVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Slf4j
@RestController
@RequestMapping("/hss/carPlan")
public class CarPlanController {

    @Autowired
    private CarPlanService carPlanService;
    @Autowired
    private HssTask hssTask;
    @Autowired
    private WorkTask workTask;

    @RequestMapping(value = "initCarAttendance", method = RequestMethod.GET)
    public void initCarAttendance() {
        hssTask.initCarAttendance();
    }


    @RequestMapping(value = "updateCleanType", method = RequestMethod.GET)
    public void updateCleanType() {
        hssTask.updateCleanType();
    }

    @RequestMapping(value = "updateTrashPickup", method = RequestMethod.GET)
    public void updateTrashPickup() {
        workTask.trashPickup();
    }


    @RequestMapping(value = "mechanicalList", method = RequestMethod.POST)
    public AjaxResult<Object> mechanicalList(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.mechanicalList(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "trashPickupList", method = RequestMethod.POST)
    public AjaxResult<Object> trashPickupList(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.trashPickupList(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "garbageTransitList", method = RequestMethod.POST)
    public AjaxResult<Object> garbageTransitList(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.garbageTransitList(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.INSERT, tableName = "car_plan", bean = CarPlan.class, beanMapper = CarPlanMapper.class, names = "planName")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public AjaxResult<Object> create(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.create(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "repeatPlanName", method = RequestMethod.POST)
    public AjaxResult<Object> repeatPlanName(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.repeatPlanName(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "car_plan", idKey = "carPlanIdList", bean = CarPlan.class, beanMapper = CarPlanMapper.class, names = "planName")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.update(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "car_plan", idKey = "carPlanIdList", bean = CarPlan.class, beanMapper = CarPlanMapper.class, names = "planName")
    @RequestMapping(value = "updateIsEnable", method = RequestMethod.POST)
    public AjaxResult<Object> updateIsEnable(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.updateIsEnable(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "read", method = RequestMethod.POST)
    public AjaxResult<Object> read(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.read(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "car_plan", idKey = "carPlanIdList", bean = CarPlan.class, beanMapper = CarPlanMapper.class, names = "planName")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public AjaxResult<Object> delete(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.delete(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "work_group", idKey = "workGroupIdList", bean = WorkGroup.class, beanMapper = WorkGroupMapper.class, names = "name")
    @RequestMapping(value = "deleteWorkGroup", method = RequestMethod.POST)
    public AjaxResult<Object> deleteWorkGroup(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.deleteWorkGroup(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "work_rule", idKey = "workRuleIdList", bean = WorkRule.class, beanMapper = WorkRuleMapper.class, names = "ruleName")
    @RequestMapping(value = "deleteWorkRule", method = RequestMethod.POST)
    public AjaxResult<Object> deleteWorkRule(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            return AjaxResult.success(carPlanService.deleteWorkRule(carPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "车辆排班导出接口", notes = "车辆排班导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JxqsPlanExportVo.class)
    })
    @PostMapping("carPlanExcelExport")
    public com.hskn.hss.core.domain.AjaxResult excelExport(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {

            if (null != carPlanTo.getIds() && carPlanTo.getIds().isEmpty()) {
                return com.hskn.hss.core.domain.AjaxResult.error("请选择至少一条数据导出");
            }
            List<CarPlan> carPlanList = carPlanService.excelExport(carPlanTo);

            List exportVoList = new ArrayList<>();
            if (carPlanTo.getType() == 1) {
                carPlanList.forEach(carPlan -> {
                    JxqsPlanExportVo jxqsPlanExportVo = new JxqsPlanExportVo();
                    jxqsPlanExportVo.setPlanName(carPlan.getPlanName());
                    jxqsPlanExportVo.setGridName(carPlan.getGridName());
                    jxqsPlanExportVo.setWorkType(carPlan.getWorkTypeName());
                    jxqsPlanExportVo.setWorkCar(carPlan.getCarNumbers());
                    jxqsPlanExportVo.setWorkPeriod(carPlan.getWorkCycle() + "天");
                    jxqsPlanExportVo.setWorkGroupName(carPlan.getWorkGroupName());
                    jxqsPlanExportVo.setTangCi(carPlan.getWorkTrip());
                    jxqsPlanExportVo.setStartTime(carPlan.getWorkStartDate());
                    jxqsPlanExportVo.setEndTime(carPlan.getWorkEndDate());
                    exportVoList.add(jxqsPlanExportVo);
                });
                //工具类实例化
                ExcelUtil<JxqsPlanExportVo> util = new ExcelUtil<>(JxqsPlanExportVo.class);
                //导出
                return util.exportExcel(exportVoList, "机械作业排班计划");

            } else if (carPlanTo.getType() == 2) {
                for (CarPlan carPlan : carPlanList) {
                    LjqyPlanExportVo ljqyPlanExportVo = new LjqyPlanExportVo();
                    ljqyPlanExportVo.setPlanName(carPlan.getPlanName());
                    ljqyPlanExportVo.setGridName(carPlan.getGridName());
                    ljqyPlanExportVo.setWorkCar(carPlan.getCarNumbers());
                    ljqyPlanExportVo.setCollectionPoint(carPlan.getCollectPointNames());
                    ljqyPlanExportVo.setWorkCycle(carPlan.getWorkCycle() + "天");
                    ljqyPlanExportVo.setWorkGroupName(carPlan.getWorkGroupName());
                    ljqyPlanExportVo.setStartTime(carPlan.getWorkStartDate());
                    ljqyPlanExportVo.setEndTime(carPlan.getWorkEndDate());
                    exportVoList.add(ljqyPlanExportVo);
                }
                //工具类实例化
                ExcelUtil<LjqyPlanExportVo> util = new ExcelUtil<>(LjqyPlanExportVo.class);
                //导出
                return util.exportExcel(exportVoList, "垃圾清运排班计划");
            } else {
                for (CarPlan carPlan : carPlanList) {
                    LjzyPlanExportVo ljzyPlanExportVo = new LjzyPlanExportVo();
                    ljzyPlanExportVo.setPlanName(carPlan.getPlanName());
                    ljzyPlanExportVo.setGridName(carPlan.getGridName());
                    ljzyPlanExportVo.setWorkCar(carPlan.getCarNumbers());
                    ljzyPlanExportVo.setCollectingStations(carPlan.getCollectStandNames());
                    ljzyPlanExportVo.setTreatmentStation(carPlan.getProcessStandNames());
                    ljzyPlanExportVo.setWorkGroupName(carPlan.getWorkGroupName());
                    ljzyPlanExportVo.setStartTime(carPlan.getWorkStartDate());
                    ljzyPlanExportVo.setEndTime(carPlan.getWorkEndDate());
                    exportVoList.add(ljzyPlanExportVo);
                }
                //工具类实例化
                ExcelUtil<LjzyPlanExportVo> util = new ExcelUtil<>(LjzyPlanExportVo.class);
                //导出
                return util.exportExcel(exportVoList, "垃圾转运排班计划");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }


    //    @SysLog(businessType = BusinessType.INSERT, tableName = "car_plan", bean = CarPlan.class, beanMapper = CarPlanMapper.class, names = "planName")
    @ApiOperation(value = "车辆排班导入接口", notes = "车辆排班导入")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @PostMapping("carPlanExcelImport")
    public AjaxResult carPlanExcelImport(@Validated CarPlanExcelReaderTO carPlanExcelReaderTO) {
        try {
            MultipartFile file = carPlanExcelReaderTO.getFiles();
            if (Arrays.stream(ExcelTypeEnum.values()).noneMatch(s -> s.getName().equals(FileTypeUtils.getFileType(file.getOriginalFilename().toLowerCase())))) {
                return AjaxResult.fail("导入文件格式错误");
            }
            InputStream inputStream = new BufferedInputStream(file.getInputStream());

            ExcelUtil<CarPlan> util = new ExcelUtil<CarPlan>(CarPlan.class);

            List<CarPlan> carPlanList = util.importExcel(inputStream);
            if (carPlanList.isEmpty()) {
                return AjaxResult.fail("导入内容为空");
            }
            carPlanService.carPlanExcelImport(carPlanList, carPlanExcelReaderTO.getType());

            if (carPlanService.saveBatch(carPlanList)) {
                return AjaxResult.success();
            } else {
                return AjaxResult.fail("导入失败");
            }
        } catch (ExcelReaderDataException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.fail();
        }
    }

    @ApiOperation(value = "车辆排班导出模板接口", notes = "车辆排班模板导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok")
    })
    @PostMapping("carPlanExcelExportTemplate")
    public com.hskn.hss.core.domain.AjaxResult carPlanExcelExportTemplate(@RequestBody @Validated CarPlanTo carPlanTo) {
        try {
            if (carPlanTo.getType() == 1) {
                //工具类实例化
                ExcelUtil<JxqsPlanExportVo> util = new ExcelUtil<JxqsPlanExportVo>(JxqsPlanExportVo.class);
                //数据载体转换
                List<JxqsPlanExportVo> jxqsPlanExportVoList = new ArrayList<>();
                //导出
                return util.exportExcel(jxqsPlanExportVoList, "机械作业模板");
            } else if (carPlanTo.getType() == 2) {
                //工具类实例化
                ExcelUtil<LjqyPlanExportVo> util = new ExcelUtil<LjqyPlanExportVo>(LjqyPlanExportVo.class);
                //数据载体转换
                List<LjqyPlanExportVo> ljqyPlanExportVoList = new ArrayList<>();

                return util.exportExcel(ljqyPlanExportVoList, "垃圾清运模板");
            } else {
                //工具类实例化
                ExcelUtil<LjzyPlanExportVo> util = new ExcelUtil<LjzyPlanExportVo>(LjzyPlanExportVo.class);
                //数据载体转换
                List<LjzyPlanExportVo> ljzyPlanExportVoList = new ArrayList<>();

                return util.exportExcel(ljzyPlanExportVoList, "垃圾转运模板");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }
    }

    @ApiOperation(value = "机械作业统计接口", notes = "机械作业统计")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PageVo.class)
    })
    @PostMapping("mechanicalWorkPlanCount")
    public AjaxResult mechanicalWorkPlanCount(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            BaseEntity entity = new BaseEntity();
            return AjaxResult.success(Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), carPlanService.mechanicalWorkPlanCount(entity, carWorkPlanCountTo)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "机械作业明细接口", notes = "机械作业明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PageVo.class)
    })
    @PostMapping("mechanicalWorkPlanDetail")
    public AjaxResult mechanicalWorkPlanDetail(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), carPlanService.mechanicalWorkPlanDetail(carWorkPlanCountTo)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "机械作业车辆明细接口", notes = "机械作业车辆明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PageVo.class)
    })
    @PostMapping("mechanicalWorkPlanDetailByCarVo")
    public AjaxResult mechanicalWorkPlanDetailByCarVo(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(carPlanService.mechanicalWorkPlanDetailByCarVo(carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "垃圾清运统计接口", notes = "垃圾清运统计")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PageVo.class)
    })
    @PostMapping("trashPickupWorkPlanCount")
    public AjaxResult trashPickupWorkPlanCount(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), carPlanService.trashPickupWorkPlanCount(carWorkPlanCountTo)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "垃圾清运明细接口", notes = "垃圾清运明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PageVo.class)
    })
    @PostMapping("trashPickupWorkPlanDetail")
    public AjaxResult trashPickupWorkPlanDetail(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(Tools.page(carWorkPlanCountTo.getPage(), carWorkPlanCountTo.getSize(), carPlanService.trashPickupWorkPlanDetail(carWorkPlanCountTo)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "垃圾清运车辆明细接口", notes = "垃圾清运车辆明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PageVo.class)
    })
    @PostMapping("trashPickupWorkPlanDetailByCar")
    public AjaxResult trashPickupWorkPlanDetailByCar(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(carPlanService.trashPickupWorkPlanDetailByCar(carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "垃圾转运统计接口", notes = "垃圾转运统计")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PageVo.class)
    })
    @PostMapping("garbageTransitWorkPlanCount")
    public AjaxResult garbageTransitWorkPlanCount(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            BaseEntity entity = new BaseEntity();
            return AjaxResult.success(carPlanService.garbageTransitWorkPlanCount(entity, carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "垃圾转运明细接口", notes = "垃圾转运明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = PageVo.class)
    })
    @PostMapping("garbageTransitWorkPlanDetail")
    public AjaxResult garbageTransitWorkPlanDetail(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(carPlanService.garbageTransitWorkPlanDetail(carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    //    @RequestMapping(value = "updateCarTrackByDistanceBetween", method = RequestMethod.POST)
//    public AjaxResult<Object> updateCarTrackByDistanceBetween(@RequestBody @Validated JxqsWorkPlanCountTo jxqsWorkPlanCountTo) {
//        try {
//            jxqsPlanService.updateCarTrackByDistanceBetween(jxqsWorkPlanCountTo);
//            return AjaxResult.success("更新成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("报错ERROR", e);
//            return AjaxResult.fail("操作失败");
//        }
//    }
//
//    @RequestMapping(value = "updateCarTrackByOil", method = RequestMethod.POST)
//    public AjaxResult<Object> updateCarTrackByOil(@RequestBody @Validated JxqsWorkPlanCountTo jxqsWorkPlanCountTo) {
//        try {
//            jxqsPlanService.updateCarTrackByOil(jxqsWorkPlanCountTo);
//            return AjaxResult.success("更新成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("报错ERROR", e);
//            return AjaxResult.fail("操作失败");
//        }
//    }
//
    @RequestMapping(value = "updateCarTrackNowProgress", method = RequestMethod.POST)
    public AjaxResult<Object> updateCarTrackNowProgress(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            carPlanService.updateCarTrackNowProgress(carWorkPlanCountTo);
            return AjaxResult.success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "getCarShiftStatus", method = RequestMethod.POST)
    public AjaxResult<Object> getCarShiftStatus(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(carPlanService.getCarShiftStatus(carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "copyGrid", method = RequestMethod.POST)
    public AjaxResult<Object> copyGrid() {
        try {
            carPlanService.copyGrid();
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "ssCarPlanWork", method = RequestMethod.POST)
    public AjaxResult<Object> ssCarPlanWork() {
        try {
            return AjaxResult.success(carPlanService.ssCarPlanWork());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "qsCarPlanWork", method = RequestMethod.POST)
    public AjaxResult<Object> qsCarPlanWork() {
        try {
            return AjaxResult.success(carPlanService.qsCarPlanWork());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "ssCarPlanWorkList", method = RequestMethod.POST)
    public AjaxResult<Object> ssCarPlanWorkList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return AjaxResult.success(carPlanService.ssCarPlanWorkList(params));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "qsCarPlanWorkList", method = RequestMethod.POST)
    public AjaxResult<Object> qsCarPlanWorkList(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return AjaxResult.success(carPlanService.qsCarPlanWorkList(params));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "updateRptCarWorkByTimeConsuming1", method = RequestMethod.POST)
    public AjaxResult<Object> updateRptCarWorkByTimeConsuming1(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            carPlanService.updateRptCarWorkByTimeConsuming(carWorkPlanCountTo.getDate());
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "insertGridSnapshot", method = RequestMethod.POST)
    public AjaxResult<Object> insertGridSnapshot(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            carPlanService.insertGridSnapshot(carWorkPlanCountTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "updateTrashPickupWork", method = RequestMethod.POST)
    public AjaxResult<Object> updateTrashPickupWork() {
        try {
            carPlanService.updateTrashPickupWork(DateUtil.format(new Date(), "yyyy-MM-dd"));
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "createRptCarWork", method = RequestMethod.POST)
    public AjaxResult<Object> createRptCarWork(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            carPlanService.createRptCarWork(carWorkPlanCountTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "updateCleanType", method = RequestMethod.POST)
    public AjaxResult<Object> updateCleanType(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            carPlanService.updateCleanType(carWorkPlanCountTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "workPlanByCollectStand", method = RequestMethod.POST)
    public AjaxResult<Object> garbageTransitWorkPlanByCollectStand(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(carPlanService.garbageTransitWorkPlanByCollectStand(carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "workPlanByProcessStand", method = RequestMethod.POST)
    public AjaxResult<Object> garbageTransitWorkPlanByProcessStand(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(carPlanService.garbageTransitWorkPlanByProcessStand(carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "mileageComplete", method = RequestMethod.POST)
    public AjaxResult<Object> mileageComplete(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            carPlanService.mileageComplete(carWorkPlanCountTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "updateRptCarWorkByTimeConsuming2", method = RequestMethod.POST)
    public AjaxResult<Object> updateRptCarWorkByTimeConsuming2(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            carPlanService.updateRptCarWorkByTimeConsuming2(carWorkPlanCountTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "updateStand", method = RequestMethod.POST)
    public AjaxResult<Object> updateStand() {
        try {
            carPlanService.updateStand("2021-12-23");
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "updateRptCarWorkByGarbageTransitWork", method = RequestMethod.POST)
    public AjaxResult<Object> updateRptCarWorkByGarbageTransitWork() {
        try {
            carPlanService.updateRptCarWorkByGarbageTransitWork("2021-12-23");
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "getExpectMileage", method = RequestMethod.POST)
    public AjaxResult<Object> getExpectMileage(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(carPlanService.getExpectMileage(carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "updateTrashPickup", method = RequestMethod.POST)
    public AjaxResult<Object> updateTrashPickup(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            carPlanService.updateTrashPickup(carWorkPlanCountTo.getDate());
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "garbageTransitWorkPlanDetailByCar", method = RequestMethod.POST)
    public AjaxResult<Object> garbageTransitWorkPlanDetailByCar(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            return AjaxResult.success(carPlanService.garbageTransitWorkPlanDetailByCar(carWorkPlanCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @ApiOperation(value = "机械清扫作业汇总导出接口", notes = "机械清扫作业汇总导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JxqsWorkPlanCountVo.class)
    })
    @PostMapping("mechanicalWorkPlanCountExport")
    public com.hskn.hss.core.domain.AjaxResult mechanicalWorkPlanCountExport(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            BaseEntity entity = new BaseEntity();
            List<JxqsWorkPlanCountVo> jxqsWorkPlanCountVoList = carPlanService.mechanicalWorkPlanCount(entity, carWorkPlanCountTo);
            //工具类实例化
            ExcelUtil<JxqsWorkPlanCountVo> util = new ExcelUtil<>(JxqsWorkPlanCountVo.class);
            //导出
            return util.exportExcel(jxqsWorkPlanCountVoList, "机械清扫作业汇总");
        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

    @ApiOperation(value = "机械清扫作业明细导出接口", notes = "机械清扫作业明细导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JxqsWorkPlanCountVo.class)
    })
    @PostMapping("mechanicalWorkPlanDetailExport")
    public com.hskn.hss.core.domain.AjaxResult mechanicalWorkPlanDetailExport(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            List<JxqWorkPlanDetailVo> jxqWorkPlanDetailVoList = carPlanService.mechanicalWorkPlanDetail(carWorkPlanCountTo);
            //工具类实例化
            ExcelUtil<JxqWorkPlanDetailVo> util = new ExcelUtil<>(JxqWorkPlanDetailVo.class);
            //导出
            return util.exportExcel(jxqWorkPlanDetailVoList, "机械清扫作业明细");
        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

    @ApiOperation(value = "垃圾清运作业汇总导出接口", notes = "垃圾清运作业汇总导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JxqsWorkPlanCountVo.class)
    })
    @PostMapping("trashPickupWorkPlanCountExport")
    public com.hskn.hss.core.domain.AjaxResult trashPickupWorkPlanCountExport(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            List<LjqyWorkPlanCountVo> ljqyWorkPlanCountVoList = carPlanService.trashPickupWorkPlanCount(carWorkPlanCountTo);
            //工具类实例化
            ExcelUtil<LjqyWorkPlanCountVo> util = new ExcelUtil<>(LjqyWorkPlanCountVo.class);
            //导出
            return util.exportExcel(ljqyWorkPlanCountVoList, "垃圾清运作业汇总");
        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }

    @ApiOperation(value = "垃圾清运作业明细导出接口", notes = "垃圾清运作业明细导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = JxqsWorkPlanCountVo.class)
    })
    @PostMapping("trashPickupWorkPlanDetailExport")
    public com.hskn.hss.core.domain.AjaxResult trashPickupWorkPlanDetailExport(@RequestBody @Validated CarWorkPlanCountTo carWorkPlanCountTo) {
        try {
            List<LjqyWorkPlanDetailVo> ljqyWorkPlanDetailVoList = carPlanService.trashPickupWorkPlanDetail(carWorkPlanCountTo);
            //工具类实例化
            ExcelUtil<LjqyWorkPlanDetailVo> util = new ExcelUtil<>(LjqyWorkPlanDetailVo.class);
            //导出
            return util.exportExcel(ljqyWorkPlanDetailVoList, "垃圾清运作业明细");
        } catch (Exception e) {
            e.printStackTrace();
            return com.hskn.hss.core.domain.AjaxResult.error();
        }

    }
}

